package kr.hkit.summary_java.erp.serviceui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.erp.dao.CodeTitleDao;
import kr.hkit.summary_java.erp.dao.DaoInterface;
import kr.hkit.summary_java.erp.dto.CodeTitle;

public class PanelCodeTitleUI extends JPanel implements ActionListener {
	private InputPanel pTitleno;
	private InputPanel pTitleName;
	private JPanel panel;
	private JPanel panel_1;
	private JButton btn;

	private Connection con = ConnectionFactory.getConnection();
	private DaoInterface<CodeTitle> dao = new CodeTitleDao(con);

	public void setBtn(String s) {
		this.btn.setText(s);
	}

	public void setDao(DaoInterface<CodeTitle> dao) {
		this.dao = dao;
	}

	/**
	 * Create the panel.
	 */
	public PanelCodeTitleUI(String s) {
		initialize(s);
	}

	private void initialize(String s) {

		setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(new GridLayout(0, 1, 0, 0));

		pTitleno = new InputPanel("직책번호");
		panel.add(pTitleno);
		pTitleno.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);
				CodeTitle d = null;
				try {
					d = dao.searchItem(new CodeTitle(Integer.parseInt(pTitleno
							.getTf().getText().trim())));
					if (d != null) {
						pTitleno.setTf(d.getTitleNo() + "");
						pTitleName.setTf(d.getTitleName());
						if (btn.getText() == "검색") {
							panelEditable(false);
						}
					} else if (btn.getText() != "추가") {
						JOptionPane.showMessageDialog(PanelCodeTitleUI.this,
								"없는 정보");
						clsPanel();
					}
				} catch (NumberFormatException | SQLException e1) {
					e1.printStackTrace();
				}
			}
		});

		pTitleName = new InputPanel("직책명");
		panel.add(pTitleName);

		panel_1 = new JPanel();
		add(panel_1, BorderLayout.SOUTH);

		btn = new JButton();
		setBtn(s);
		btn.addActionListener(this);
		panel_1.add(btn);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn) {
			try {
				actionPerformedBtnNewButton(e);
			} catch (SQLException e1) {
				System.out.println("Fail!");
			}
		}
	}

	protected void actionPerformedBtnNewButton(ActionEvent e)
			throws SQLException {
		int dno;
		String titleName;
		int floor;
		CodeTitle d = null;
		switch (btn.getText()) {
			case "추가":
				panelEditable(true);
				dno = Integer.parseInt(pTitleno.getTf().getText().trim());
				titleName = pTitleName.getTf().getText();
				d = new CodeTitle(dno, titleName);
				dao.insertItem(d);
				JOptionPane.showMessageDialog(this, "추가 성공");
				clsPanel();
				break;
			case "삭제":
				panelEditable(true);
				dno = Integer.parseInt(pTitleno.getTf().getText().trim());
				d = new CodeTitle(dno);
				dao.deleteItemByNo(d);
				JOptionPane.showMessageDialog(this, "삭제 성공");
				clsPanel();
				break;
			case "수정":
				panelEditable(true);
				dno = Integer.parseInt(pTitleno.getTf().getText().trim());
				titleName = pTitleName.getTf().getText();
				d = new CodeTitle(dno, titleName);
				dao.updateItemByNo(d);
				JOptionPane.showMessageDialog(this, "수정 성공");
				clsPanel();
				break;
			case "검색":
				panelEditable(true);
				dno = Integer.parseInt(pTitleno.getTf().getText().trim());
				d = dao.searchItem(new CodeTitle(dno));
				if (d != null) {
					pTitleno.setTf(d.getTitleNo() + "");
					pTitleName.setTf(d.getTitleName());
					panelEditable(false);
				} else {
					JOptionPane.showMessageDialog(this, "없는 정보");
					clsPanel();
				}
				break;
		}
	}

	private void panelEditable(boolean set) {
		if (set) {
			pTitleName.getTf().setEditable(true);
			pTitleno.getTf().setEditable(true);
		} else {
			pTitleName.getTf().setEditable(false);
			pTitleno.getTf().setEditable(false);
		}
	}

	private void clsPanel() {
		pTitleno.setTf("");
		pTitleName.setTf("");
	}
}
